package com.arcgis.app.listener;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;

import com.arcgis.app.MainActivity;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GeoTrackTouchListener extends DefaultMapViewOnTouchListener {

    MainActivity mainActivity;
    public GeoTrackTouchListener(Context context, MapView mapView) {
        super(context, mapView);
        mainActivity = (MainActivity)context;
    }


    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        android.graphics.Point screenPoint=new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY()));
        Point mapPoint = mMapView.screenToLocation(screenPoint);

        if(mMapView.getCallout().isShowing()){
            mMapView.getCallout().dismiss();

            return false;
        }

        GraphicsOverlay pointOverlay = mainActivity.getPointOverLay();
        GraphicsOverlay lineOverlay = mainActivity.getLineOverLay();
        GraphicsOverlay polygonOverlay = mainActivity.getPolygonOverLay();

//        List<GraphicsOverlay> overlays = new ArrayList<GraphicsOverlay>();

        Map findedMap =  null;
        int index = -1;
        String str  = "";

        //优先查面
        if (polygonOverlay.isVisible()){
            int polygonOverlayLen = polygonOverlay.getGraphics().size();

            boolean contains = false;

            for(int i = 0;i<polygonOverlayLen;i++){
                Graphic g = polygonOverlay.getGraphics().get(i);

                Geometry geometry = g.getGeometry();

                if(geometry.getSpatialReference().getWkid() != mapPoint.getSpatialReference().getWkid()){

                    geometry = GeometryEngine.project(geometry, SpatialReference.create(mapPoint.getSpatialReference().getWkid()));
                }
                contains = GeometryEngine.contains(geometry,mapPoint) || GeometryEngine.crosses(geometry,mapPoint);

                if(contains){
                    Polygon polygon = (Polygon)geometry;
                    str = polygon.toJson();
                    Log.i("findMap Geometry: ",str);
                    findedMap = g.getAttributes();
                    index = i;
                    break;
                }
            }

            if (findedMap != null){
                mainActivity.showCalloutPolygon(str);
                return super.onSingleTapConfirmed(e);
            }
        }

        if (lineOverlay.isVisible()){
            int lineOverlayLen = lineOverlay.getGraphics().size();
            boolean contains = false;
            for(int i = 0;i< lineOverlayLen;i++){
                Graphic g = lineOverlay.getGraphics().get(i);
                Geometry geometry = g.getGeometry();

                if(geometry instanceof Polyline){

                    double distince = GeometryEngine.distanceBetween(mapPoint,geometry);

                    if(distince < 30){

                        contains = true;
                        index = i;

                    }

                }

                if(contains){
                    Polyline polyline = (Polyline)geometry;
                    str = polyline.toJson();
                    Log.i("findMap Geometry: ",str);
                    findedMap = g.getAttributes();

                    break;
                }
            }

            if(findedMap != null){
                mainActivity.showCalloutLine(str);
                return super.onSingleTapConfirmed(e);
            }


        }

        Point point;
        Geometry geometry_3857 = null;
        if (pointOverlay.isVisible()){
            int pointOverlayLen = pointOverlay.getGraphics().size();
            boolean contains = false;
            for(int i = 0;i< pointOverlayLen;i++){
                Graphic g = pointOverlay.getGraphics().get(i);
                Geometry geometry = g.getGeometry();

                if(geometry instanceof  Point){
                    geometry_3857 = GeometryEngine.project(geometry,SpatialReference.create(3857));

                    double distince = GeometryEngine.distanceBetween(mapPoint,geometry_3857);

                    if(distince < 30){

                        contains = true;

                    }
                }


                if(contains){
                    Point point1 = (Point) geometry;
                    str = point1.toJson();
                    Log.i("findMap Geometry: ",str);
                    mainActivity.showCalloutPoint(geometry.toJson());
                    findedMap = g.getAttributes();

                    break;
                }
            }
        }

        if(findedMap != null){

//            mainActivity.(findedMap);

            return super.onSingleTapConfirmed(e);
        }


        return super.onSingleTapConfirmed(e);
    }
}
